package cn.ldh.xhr_01;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class Producer {

    public static void main(String[] args) throws Exception {

        // 1 创建生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producer_hello");

        // 2 设置nameServer地址
        producer.setNamesrvAddr("localhost:9876");

        // 3 启动
        producer.start();

        // 创建消息
        Message message = new Message("xhr_topic", "hello", "我要成为世界首富！".getBytes(StandardCharsets.UTF_8));

        // 发送消息 并且响应结果

        producer.send(message,new SendCallback(){

            @Override
            public void onSuccess(SendResult sendResult) {

                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {

                System.out.println("系统异常"+throwable.getMessage());

            }
        });

        // 关闭
        //producer.shutdown();




    }
}
